// pages/management/bill-income/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: 0,
    showDate: false,
    radio: "",
    value: "",
    typeList: [],
    accountBookId: 0,
    amount: "",
    financeId: 0,
    financeName: "",
    grossWeight: "",
    netWeight: "",
    status: 0,
    recordTime: wx.$dayjs().format("YYYY-MM-DD HH:mm:ss")
  },

  // 查看收支记录详情
  getDetail() {
    wx.showLoading({
      title: "查询中..."
    });
    wx.$apis.getAccountBookRecordInfo(this.data.recordId).then((res) => {
      const { unitPrice, amount, grossWeight, netWeight, status, financeId, financeName } = res.data;
      this.setData({ unitPrice: wx.$getYuan(unitPrice), amount: wx.$getYuan(amount), grossWeight, netWeight, status, financeId, financeName, radio: financeId });
      if (financeName === "买菜" || financeName === "卖菜") {
        this.setData({
          type: 1
        });
      }
    });
  },

  onDate() {
    this.setData({
      showDate: true
    });
  },

  onConfirm(event) {
    this.setData({
      showDate: false,
      recordTime: wx.$dayjs(event.detail).format("YYYY-MM-DD HH:mm:ss")
    });
  },

  onClose() {
    this.setData({
      showDate: false
    });
  },

  onChangeRadioType(e) {
    this.setData({
      radio: e.detail,
      // amount: "",
      unitPrice: "",
      grossWeight: "",
      netWeight: ""
    });
    const { typeList } = this.data;
    let Index = typeList.findIndex((item) => item.id == e.detail);
    if (Index > -1) {
      let name = typeList[Index].name;
      if (name === "买菜" || name === "卖菜") {
        this.setData({
          type: 1
        });
      } else {
        this.setData({
          type: 0
        });
      }
    } else {
      this.setData({
        type: 0
      });
    }
  },

  onChangeRadioStatus(e) {
    this.setData({
      status: e.detail
    });
  },

  onSubmit() {
    let { radio, typeList, accountBookId, amount, grossWeight, netWeight, status, recordTime, unitPrice } = this.data;
    let financeName = "";
    let financeId = 0;
    let typeIndex = typeList.findIndex((item) => item.id == radio);
    if (typeIndex > -1) {
      financeId = typeList[typeIndex].id;
      financeName = typeList[typeIndex].name;
    } else {
      return wx.showToast({
        title: "请重新选择用途",
        icon: "error"
      });
    }
    let params = {
      id: this.data.recordId,
      accountBookId,
      amount: wx.$getFen(amount),
      unitPrice: wx.$getFen(unitPrice),
      financeId,
      financeName,
      grossWeight,
      netWeight,
      status,
      recordTime,
      type: 1
      // orderId: 0,
      // orderType: 0,
    };

    let api = "";
    if (!params.id) {
      api = "addAccountBookRecord";
    } else {
      api = "editAccountBookRecord";
    }
    wx.$apis[api](params).then((res) => {
      wx.navigateBack();
    });
  },

  // 获取财务类型列表
  getFinanceTypeList() {
    return new Promise((reslove, reject) => {
      let params = {
        current: 1,
        size: 100,
        status: 1
      };
      wx.$apis.getFinanceTypeList(params).then((res) => {
        if (res.data) {
          const { records } = res.data;
          this.setData({
            typeList: records
          });
          reslove();
        }
      });
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      accountBookId: options.id,
      recordId: options.recordId || ""
    });

    this.getFinanceTypeList().then(() => {
      if (options.recordId) {
        this.getDetail();
      }
    });
  }
});
